<?php
session_start();
if(!isset($_SESSION['id'])){
    header('Location: login.php');
    exit;
}

include_once("../controller/nivel_controller_class.php");
include_once("../controller/opcoes_sistema_controller_class.php");
include_once("../controller/permissoes_controller_class.php");

$objNivel = new NivelController();
$listNivel = $objNivel->selecionar();

$objOpc = new OpcoesSistemaController();
$listOpc = $objOpc->selecionar();

if(isset($_POST['cadastrar'])){
    $nivel = $_POST['nivel'];
    $opcoes = @$_POST['chkOpc'];
    
    if($nivel == 0){
        echo "<script>alert('Selecione um Nivel')</script>";
    }elseif(count($opcoes) <= 0){
        echo "<script>alert('Selecione pelo menos Uma Opção')</script>";
    }else{
        
        $objPermissao = new PermissoesController();
        
        if($objPermissao->cadastrar($nivel, $opcoes)){
            echo "<script>alert('OK')</script>";
        }else{
            echo "<script>alert('Erro')</script>";
        }
        
    }
}

?>
<!DOCTYPE html>
<html lang="en" class="no-js">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
        <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
        <title>::: SOS UNITAU -- Sistema de ordem de serviço</title>

        <link rel="shortcut icon" href="img/logo.jpg">
        <link rel="stylesheet" type="text/css" href="css/normalize.css" />
        <link rel="stylesheet" type="text/css" href="css/demo.css" />
        <link rel="stylesheet" type="text/css" href="css/icons.css" />
        <link rel="stylesheet" type="text/css" href="css/style2.css" />
        <link rel="stylesheet" type="text/css" href="css/StylePage.css" />
        <link rel="stylesheet" type="text/css" href="css/default.css" />
        <link rel="stylesheet" type="text/css" href="css/component.css" />
        <script type="text/javascript" src="js/jquery1.6.min.js"></script>
        <script src="js/jquery.maskedinput-1.3.min.js"></script>

<!--        <script type="text/javascript"> PARA NAO CONFLITAR 

            var $JQuery = jQuery.noConflict();
        </script>-->

        <script>
            $(document).ready(function(){
                $('#nivel').change(function(){
                    var niv = $('#nivel').val();
                    
                    $.post("../funcoes/pemissoesPorNivel.php", {niv: niv}, function (data) {
                        $('#table_check').find('tr').remove();
                        
                        $.each(data, function (key, value) {
//                            alert(value.id + " - " + value.descricao);
                            $('#table_check').append("<tr><td> <input name='chkOpc[]' "+
                                    value.checked +" id='chkOpc[]' value='"+ 
                                    value.id +"' type='checkbox' />&nbsp; &nbsp;"+ value.descricao +"</td></tr>");
                        });
                        
                    },'json').fail(function(){
                        alert("foda");
                    });
                    
                });
            });


        </script>
        

    </head>
    <body>

        <div class="loading"></div>
        <div class="container"> <!-- container -->



            <!-----------------------------CONTEUDO DA PÁGINA --------------------------------->
            <!------- SEGUE ABAIXO ------->



            <div class="barra-superior"></div>
            <div class="sobre-empresa">
                Powered by: Grupo NOP
            </div>

            <!------------- CONTEUDO DE FORMULARIOS---------------------->

            <div class="titulo-page">
                ::: Manipular Níveis
            </div>






            <form name="manipulaNivel" id="manipulaNivel" method="post" enctype="multipart/form-data" action="" class="style-form">

                <fieldset style="width:30px;">
                    <legend>Informe o Nível abaixo: </legend>
                    <table width="800px" class="table-form">
                        <tr>
                            <td> 
                                <label for="txtMaisNivel"> Incluir Nível: </label> 
                            </td>
                            <td> 
                                <input name="txtMaisNivel" id="txtMaisNivel" type="text" placeholder="Ex: Administrador" />
                            </td>
                        </tr>
                        <tr>
                            <td> 
                                <label for="txtMaisNivel"> Selecionar Nível: </label> 
                            </td>
                            <td> 
                                <select name="nivel" id="nivel">
                                    <option value="0">Selecione</option>
                                    <?php if (count($listNivel) > 0) { ?>
                                        <?php foreach ($listNivel as $niv) { ?>
                                            <option value="<?= $niv->getId() ?>"><?= $niv->getDescricao() ?></option>
                                        <?php }
                                    } ?>

                                </select>
                            </td>
                        </tr>

                    </table>
                </fieldset>
                <div class="clear"></div>   
                <fieldset style="width:300px">
                    <legend> Determine as Permissões abaixo: </legend>
                    <table width="800px" class="table-form" id="table_check">
                        <?php if (count($listOpc) > 0) { ?>
                            <?php foreach ($listOpc as $opc) { ?>
                                <tr>
                                    <td> <input name="chkOpc[]" id="chkOpc[]" value="<?= $opc->getId() ?>" type="checkbox" />&nbsp; &nbsp;<?= $opc->getDescricao() ?></td>
                                </tr>

                            <?php }
                        } ?>
                    </table>

                </fieldset>

                <div class="clear"></div>   
                <input type="submit" value="Gravar" name="cadastrar" class="botao"  style="display:inline-block; margin-left:50%; margin-top:3%;" /><input type="button" value="Cancelar" style="margin-left:5%;" onClick="javascript:c = window.confirm('Deseja Realmente cancelar?');
                        if (c == true) {
                            self.close();
                        } else {
                        }" class="botao" />    


            </form>
            
            
                 <?php include_once("menuLateral.php"); ?>
            
            
            
        </div> <!-- FECHA CONTAINER -->

        <div class="barra-superior" style="margin-top:193px; height:80px; margin-left:80px"></div>





    </body>
<script src="js/classie.js"></script>
<script src="js/borderMenu.js"></script>
</html>